'''
Master task

Registers slave tasks.
'''

import sys
import traceback

import dexen

import conditions
import settings
from individual import Individual
from slave_tasks.dev import DevTask
from slave_tasks.eval import EvalAreaTask,EvalVolTask
from slave_tasks.feedback import FeedbackTask

# Master task
class MasterTask(dexen.BaseMasterTask):
    def __init__(self, master_env):
        #register slave tasks
        try:
            assert isinstance(master_env, dexen.MasterEnv)
            master_env.start_task(DevTask, "Development", settings.DEV_SIZE, conditions.dev_cond)
            master_env.start_task(EvalAreaTask, "Eval Area", settings.EVAL_AREA_SIZE, conditions.eval_area_cond)
            master_env.start_task(EvalVolTask, "Eval Volume", settings.EVAL_VOL_SIZE, conditions.eval_vol_cond)
            master_env.start_task(FeedbackTask, "Feedback", settings.FEED_SIZE, conditions.feedback_cond)
        except:
            traceback.print_tb(sys.exc_info()[2])
        #initialise population
        for _ in range(settings.POP_SIZE):
            ind = Individual()
            master_env.set_ind(ind)
        
    def __execute__(self, master_env):
        assert isinstance(master_env, dexen.MasterEnv)
        pop_size = master_env.num_inds()
        if pop_size >= settings.MAX_BIRTHS:
            master_env.stop_master()
